package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.Alias;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin/setmeal")
@Slf4j
@Api(tags = "套餐模块接口")
@Alias("adminSetmealController")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    @ApiOperation("套餐模块分页查询的业务接口")
    public Result<PageResult> getSetmealByPage( SetmealPageQueryDTO dto){
        Result<PageResult> result = setmealService.getSetmealByPage(dto);

        return result;

    }

    @PostMapping
    @ApiOperation("套餐模块增加功能业务接口")
    @CacheEvict(cacheNames = {"setmeal"} ,key = "#dto.categoryId") // 增加套餐后，清理对应的套餐分类缓存,重新获取最新数据
    public Result<Object> addSetmeal(@RequestBody SetmealDTO dto){
        Result<Object> result = setmealService.addSetmeal(dto);

        return result;
    }

    @GetMapping("{id}")
    @ApiOperation("套餐模块修改回显业务接口")
    public Result<Object> getSetmealAllInfoById(@PathVariable Long id){
        Result<Object> result = setmealService.getSetmealAllInfoById(id);
        return result;
    }


    @PostMapping("status/{status}")
    @ApiOperation("套餐模块停售起售的业务接口")
    @CacheEvict(value = "setmeal",allEntries = true)
    public Result<Object> setSetmealStatusById(@PathVariable Long status,@RequestParam Long id){
        Result<Object> result = setmealService.setSetmealStatusById(status,id);

        return result;
    }


    @PutMapping
    @ApiOperation("套餐模块修改套餐的业务接口")
    @CacheEvict(value = "setmeal",key = "#dto.categoryId")
    public Result<Object> setSetmealById(@RequestBody SetmealDTO dto){
        Result<Object> result = setmealService.setSetmealById(dto);
        return result;
    }

    @DeleteMapping
    @ApiOperation("套餐模块删除套餐的业务接口")
    @CacheEvict(value = "setmeal",allEntries = true)
    public Result<Object> deleteSetmealById(@RequestParam("ids") String ids){
        Result<Object> result = setmealService.deleteSetmealById(ids);

        return result;
    }

}
